<?php
namespace Common\Model;

/**
 * 客栈房间预定信息记录表
 * @modify zhangzhen 2017-2-27 创建
 */

Class LastRoomPriceModel extends \Common\Model\BaseModel
{
    protected $trueTableName = 'tb_last_room_price';

    protected function _before_insert(&$data, $options) {
    }
    protected function _before_update(&$data, $options) {
    }

    /**
     * 获取信息
     * @modify zhangzhen 2017-02-27 创建
     * @param  int    $id   
     * @param  mixin   
     * @return array   
     */
    public function getInfoById($id) {
        $option = ['id'=>$id];
        $result = $this->where($option)->find();
        return empty($result)? [] : $result;
    }

    /**
     * 获取信息
     * @modify zhangzhen 2017-02-27 创建
     * @param  mixin  $roomTypeId
     * @return array   
     */
    public function getInfoByRoomType($roomTypeId, $nowadays) {
        $option = [
            'isdelete' => NOT_DELETE,
            'roomtype_id' => $roomTypeId,
        ];
        if($nowadays){
            $option['enddate'] = ['egt',$nowadays];
        }
        $result = $this->where($option)->find();
        return empty($result)? [] : $result;
    }
    /**
     * 获取信息
     * @modify zhangzhen 2017-02-27 创建
     * @param  mixin  $roomTypeId
     * @param  int    $timestamp
     * @return array   
     */
    public function getInfoByRoomTypeBetween($roomTypeId, $nowadays) {
        $option = [
            'isdelete' => NOT_DELETE,
            'roomtype_id' => $roomTypeId,
        ];
        if($nowadays){
            $option['enddate'] = ['egt',$nowadays];
        }
        $result = $this->where($option)->find();
        return empty($result)? [] : $result;
    }
    /**
     * 获取信息
     * @modify wenhui 2017-09-26 创建
     * @param  mixin  $roomTypeId
     * @param  int    $timestamp
     * @return array   
     */
    public function getInfoByRoomTypeDate($roomTypeId, $nowadays) {
        $option = [
            'isdelete' => NOT_DELETE,
            'roomtype_id' => $roomTypeId,
        ];
        if($nowadays){
            $option['startdate'] = ['elt',$nowadays];
            $option['enddate'] = ['egt',$nowadays];
        }
        $result = $this->where($option)->find();

        return empty($result)? [] : $result;
    }
    /**
     * 新增信息
     * @modify zhangzhen 2017-02-27 创建
     * @param  mixin  $roomTypeId   房型ID
     * @param  mixin  $startDate    开始日期
     * @param  mixin  $endDate      结束日期
     * @param  mixin  $startTime    开始时间
     * @param  mixin  $endTime      结束时间
     * @param  mixin  $price        价格
     * @return id   
     */
    public function addInfo($roomTypeId, $startDate, $endDate, $startTime, $endTime, $price) {
        $data = [
            'roomtype_id' => $roomTypeId,
            'startdate' => $startDate,
            'enddate' => $endDate,
            'starttime' => $startTime,
            'endtime' => $endTime,
            'price' => $price,
        ];
        return $this->add($data);
    }

    /**
     * 获取信息
     * @modify zhangzhen 2017-02-27 创建
     * @param  mixin  $id           id
     * @param  mixin  $roomTypeId   房型ID
     * @param  mixin  $startDate    开始日期
     * @param  mixin  $endDate      结束日期
     * @param  mixin  $startTime    开始时间
     * @param  mixin  $endTime      结束时间
     * @param  mixin  $price        价格   
     * @return bool   
     */
    public function saveInfo($id, $roomTypeId, $startDate, $endDate, $startTime, $endTime, $price) {
        $options = [];
        if (!empty($roomTypeId)) {
            $options['roomtype_id'] = $roomTypeId;
        }
        if (!is_null($startDate)) {
            $options['startdate'] = $startDate;
        }
        if (!is_null($endDate)) {
            $options['enddate'] = $endDate;
        }
        if (!is_null($startTime)) {
            $options['starttime'] = $startTime;
        }
        if (!is_null($endTime)) {
            $options['endtime'] = $endTime;
        }
        if (!is_null($price)) {
            $options['price'] = $price;
        }
        if (!empty($name)) {
            $options['name'] = $name;
        }
        $result = $this->where(array('id' => $id))->save($options);
        return $result === false ? false : true;
    }
    
    /**
     * 删除信息
     * @modify zhangzhen 2017-02-27 创建
     * @param  mixin  $id
     * @return bool   
     */
    public function delInfo($id) {
        $where = [
            'id' => $id,
        ];
        $data = [
            'isdelete' => IS_DELETE
        ];
        $result = $this->where($where)->save($data);
        return $result === false ? false : true;
    }
}
